package org.arnetminer.personalized.service;

import java.util.ArrayList;
import java.util.List;

import org.arnetminer.personalized.dao.FollowDAO;
import org.arnetminer.personalized.util.Page;
import org.arnetminer.personalized.vo.Follow;
import org.arnetminer.personalized.vo.FollowType;
import org.arnetminer.personalized.vo.User;

public class FollowServiceImpl implements FollowService {

	private FollowDAO followDAO;

	public FollowDAO getFollowDAO() {
		return followDAO;
	}

	public void setFollowDAO(FollowDAO followDAO) {
		this.followDAO = followDAO;
	}

	@Override
	public boolean save(Follow follow) {
		
		try {
			followDAO.save(follow);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public FollowType getFollowType(long id) {
		
		FollowType followType = followDAO.findFollowTypeById(id);
		return followType;
	}

	@Override
	public boolean isFollow(User follower, long id, FollowType type) {
		List<String> properties = new ArrayList();
		List values = new ArrayList();
		properties.add("follower");
		values.add(follower);
		properties.add("followee");
		values.add(id);
		properties.add("type");
		values.add(type);

		List result = followDAO.findByProperties(properties, values, 0, 1);
		if (result != null && result.size() > 0) {
			return true;
		}

		return false;
	}

	@Override
	public boolean delete(Follow follow) {
		
		try {
			List<Follow> follows = followDAO.findByExample(follow);
			if (follows != null && follows.size() > 0) {
				followDAO.delete(follows.get(0));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public List getByProperties(List properties, List values, Page page) {

		List result = followDAO.findByProperties(properties, values,
				page.getPageStartRow(), page.getPageRecorders());

		return result;
	}

	@Override
	public List getByProperties(List properties, List values) {

		List result = followDAO.findByProperties(properties, values);

		return result;
	}

	@Override
	public int getCountByProperties(List properties, List values) {
		
		return followDAO.findCountByProperties(properties, values);
	}

}
